package com.zhongshengnetwork.rightbe.common.activity;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
//import android.support.annotation.Nullable;
//import android.support.v4.app.FragmentActivity;

/**
 * Created by luojingxing on 2018/8/1.
 */

import java.util.List;

import android.content.Intent;
//import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentActivity;
//import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.gyf.barlibrary.BarHide;
import com.gyf.barlibrary.ImmersionBar;
import com.zhongshengnetwork.rightbe.Adv.activity.SplashActivity;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.AppUtils;
import com.zhongshengnetwork.rightbe.lang.activity.BaseFragmentActivity;

////import cn.jiguang.analytics.android.api.JAnalyticsInterface;

public class MyBaseFragmentActivity extends FragmentActivity
{
    private String TAG = "MyBaseFragmentActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (AppUtils.getInstance().getAppStatus() == AppUtils.STATUS_FORCE_KILLED){
            //app被系统回收了，重新启动应用
            protectApp();
            return;
        }

        // 所有子类都将继承这些相同的属性,请在设置界面之后设置
        ImmersionBar.with(this)
                .statusBarColor(AppThemeUtils.getInstance().getThemeColorString())
                .navigationBarColor(AppThemeUtils.getInstance().getMainColorString()) //设置导航栏（虚拟键盘的颜色）
                .hideBar(BarHide.FLAG_SHOW_BAR)
                //.fitsSystemWindows(true)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(AppUtils.getInstance().isUserAgreeStatus() == false){
    return;
}
////JAnalyticsInterface.onPageStart(getApplicationContext(),this.getClass().getCanonicalName());
    }


    @Override
    protected void onPause() {
        super.onPause();
        if(AppUtils.getInstance().isUserAgreeStatus() == false){
    return;
}
////JAnalyticsInterface.onPageEnd(getApplicationContext(),this.getClass().getCanonicalName());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 如果你的app可以横竖屏切换，并且适配4.4或者emui3手机请务必在onConfigurationChanged方法里添加这句话
        ImmersionBar.with(this)
                .statusBarColor(AppThemeUtils.getInstance().getThemeColorString())
                .navigationBarColor(AppThemeUtils.getInstance().getMainColorString()) //设置导航栏（虚拟键盘的颜色）
                //.fitsSystemWindows(true)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.with(this).destroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        FragmentManager fragmentManager=getSupportFragmentManager();
        for(int indext=0;indext<fragmentManager.getFragments().size();indext++)
        {
            Fragment fragment=fragmentManager.getFragments().get(indext); //找到第一层Fragment
            if(fragment==null)
                Log.w(TAG, "Activity result no fragment exists for index: 0x"
                        + Integer.toHexString(requestCode));
            else
                handleResult(fragment,requestCode,resultCode,data);
        }
    }
    /**
     * 递归调用，对所有的子Fragment生效
     * @param fragment
     * @param requestCode
     * @param resultCode
     * @param data
     */
    private void handleResult(Fragment fragment,int requestCode,int resultCode,Intent data)
    {
        fragment.onActivityResult(requestCode, resultCode, data);//调用每个Fragment的onActivityResult
        Log.e(TAG, "MyBaseFragmentActivity");
        List<Fragment> childFragment = fragment.getChildFragmentManager().getFragments(); //找到第二层Fragment
        if(childFragment!=null)
            for(Fragment f:childFragment)
                if(f!=null)
                {
                    handleResult(f, requestCode, resultCode, data);
                }
        if(childFragment==null)
            Log.e(TAG, "MyBaseFragmentActivity1111");
    }

    protected void protectApp() {
//        Intent intent = new Intent(this, MainActivity.class);
//        intent.putExtra(AppUtils.KEY_HOME_ACTION, AppUtils.ACTION_RESTART_APP);
//        startActivity(intent);

        Intent intent = new Intent(this, SplashActivity.class);
        startActivity(intent);
        System.exit(0);
        finish();

//        final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);

        //杀掉以前进程
        //android.os.Process.killProcess(android.os.Process.myPid());
    }

}
